package Model
{
	import Controller.AreasController;
	
	import flash.events.Event;
	
	import mx.core.FlexGlobals;
	import mx.events.PropertyChangeEvent;

	public class SolidComponent extends SimpleComponent
	{
		
		private var _length:Number;
		private var _height:Number;
		private var _area:Number;
		private var _estimation:Number;
		private var _construction:Construction;
		private var propertyChangedEvent:PropertyChangeEvent = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
		protected var areaController:AreasController = new AreasController();
		
		public function SolidComponent(description:String="", id:String="", quantity:Number=1, length:Number = 0, height:Number = 0, estimation:Number = 0, area:Number = 0)
		{	
			super(description, id, quantity);
			this._length = length;
			this._height = height;
			this._estimation = estimation;
			this._area = area;
			this.addEventListener("internalChange", handleInternalChange);
			this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, handleProperyChangedEvent);
		}

		public function get estimation():Number
		{
			return _estimation;
		}

		public function set estimation(value:Number):void
		{
			_estimation = value;
			dispatchEvent(this.internalChangeEvent);
		}

		public function get length():Number
		{
			return _length;
		}

		public function set length(value:Number):void
		{
			_length = value;
			dispatchEvent(this.internalChangeEvent);
		}

		public function get height():Number
		{
			return _height;
		}

		public function set height(value:Number):void
		{
			_height = value;
			dispatchEvent(this.internalChangeEvent);
		}

		public function get area():Number
		{
			return _area;
		}

		public function set area(value:Number):void
		{
			_area = value;
			this.propertyChangedEvent.property = this.area;
			dispatchEvent(this.propertyChangedEvent);
		}
		
		override protected function calculateArea():void
		{
			this.area = (this.quantity * (this.length * this.height)) + this.estimation;
		}
		
		protected function handleInternalChange(event:Event):void
		{
			this.calculateArea();
			this.areaController.updateAreaOverviewStatus();
		}
	
		protected function handleProperyChangedEvent(event:PropertyChangeEvent):void
		{
			if(event.property == this.area)
			{
				this.areaController.updateSolidComponentAreaField(this);
				FlexGlobals.topLevelApplication.overviewPalette.updateAreaInputSectionData();
			}
			else if(event.property == this.construction)
			{
				this.areaController.updateOpaqueOverviewStatus();
			}
		}
		
		public function get construction():Construction
		{
			return _construction;
		}

		public function set construction(value:Construction):void
		{
			_construction = value;
			this.propertyChangedEvent.property = this.construction;
			dispatchEvent(this.propertyChangedEvent);
		}


	}
}